package pro.lepers.portfolio.text {
	import fr.digitas.flowearth.command.Batcher;
	import fr.digitas.flowearth.conf.Conf;
	import fr.digitas.flowearth.event.BatchEvent;
	import fr.digitas.flowearth.net.BatchLoaderItem;
	import fr.digitas.flowearth.text.fonts.IFontsProvider;
	import fr.digitas.flowearth.text.fonts.fontsManager;

	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;

	/**
	 * @author plepers
	 */
	public class FontLoader extends Batcher {

		
		
		
		private var _confProp : QName;
		
		public function FontLoader ( confProp : QName ) {
			_confProp = confProp;
			super( );
			
			addEventListener( BatchEvent.ITEM_COMPLETE, onFontItemComplete );
		}

		
		override public function start () : void {
			_build( Conf.getDatas( _confProp ) );
			
			super.start( );
		}

		private function _build (datas : XML) : void {
			
			var fontLoaderItem : BatchLoaderItem;
			
			var list : XMLList = datas.file;
			
			var ctx : LoaderContext = new LoaderContext( false, ApplicationDomain.currentDomain );
			for each (var fontFileData : XML in list ) {
				fontLoaderItem = new BatchLoaderItem( new URLRequest( fontFileData.text() ), ctx );
				addItem( fontLoaderItem );
			}
		}

		private function onFontItemComplete (event : BatchEvent) : void {
			var item : BatchLoaderItem = event.item as BatchLoaderItem;
			var provider : IFontsProvider = item.loader.content as IFontsProvider;
			fontsManager.registerFonts( provider );
		}
	}
}
